package com.quick.im.manager.controller;


import com.quick.im.api.user.UserApi;
import com.quick.im.common.base.BaseResponse;
import com.quick.im.common.entity.User;
import com.quick.im.common.entity.dto.TokenUserInfo;
import com.quick.im.common.enums.GlobalExceptionEnum;
import com.quick.im.manager.config.JwtUtil;
import com.quick.im.manager.interfaces.IUserService;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;


/**
 * 用户信息表(User) 控制器
 *
 * @author xhh
 * @since 2025-01-23
 */
@RestController
@Slf4j
@RequestMapping("user")
public class UserController implements UserApi {


    @Setter(onMethod_ = @Autowired)
    public IUserService iUserService;

    @Setter(onMethod_ = @Autowired)
    private AuthenticationManager authenticationManager;

    @Setter(onMethod_ = @Autowired)
    private UserDetailsService userDetailsService;

    @Setter(onMethod_ = @Autowired)
    private JwtUtil jwtUtil;

    @Override
    @PostMapping("register")
    public BaseResponse saveUser(@RequestBody User request) {
        try {
            iUserService.saveUser(request);
            return BaseResponse.success();
        } catch (Exception e) {
            return BaseResponse.fail(e.getMessage());
        }
    }

    @Override
    @PostMapping("/login")
    public BaseResponse login(@RequestBody User request) {
        try {
            User login = iUserService.login(request);
            if (login != null) {

                UserDetails userDetails = new org.springframework.security.core.userdetails.User(login.getMobile(), login.getPassword(), Collections.emptyList());
                final String jwt = jwtUtil.generateToken(userDetails);
                TokenUserInfo info = TokenUserInfo.builder()
                        .token("Bearer "+jwt).nickName(login.getNickName())
                        .avatar(login.getAvatar()).mobileNumber(login.getMobile())
                        .build();

                return BaseResponse.success(info);
            } else {
                log.info("用户登录失败：");
                return BaseResponse.fail(GlobalExceptionEnum.MOBILE_PASSWORD_ERROR.getInfo());
            }
        } catch (Exception e) {
            log.info("用户登录失败：" + e.getMessage());
            return BaseResponse.fail(GlobalExceptionEnum.MOBILE_PASSWORD_ERROR.getInfo());
        }


    }

    @Override
    @DeleteMapping("/{id}")
    public BaseResponse deleteUserById(@PathVariable Integer id) {
        return BaseResponse.success();
    }

    @Override
    @PutMapping("/")
    public BaseResponse UpdateUser(@RequestBody User request) {
        try {
            return BaseResponse.success(iUserService.updateUser(request));
        } catch (Exception e) {
            return BaseResponse.fail(e.getMessage());
        }
    }

    @Override
    @GetMapping("/list")
    public BaseResponse User(User request) {
        try {
            return BaseResponse.success(iUserService.listUser(request));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    @GetMapping("/{id}")
    public BaseResponse<User> getUserById(@PathVariable Integer id) {
        return iUserService.getUserById(id);
    }


    @GetMapping("/id")
    public BaseResponse<User> getUserById() {
        log.info("请求测试！！！！");
        return BaseResponse.success();
    }
}


